//获取id
function $(id){
	return typeof id === "string" ? document.getElementById(id) : id;
}

//获取tagName
function $$(oParent, elem){
	return (oParent || document).getElementsByTagName(elem);
}

//获取className
function $$$(oParent, sClass){
	var aElem = $$(oParent, '*');
	var aClass = [];
	var i = 0;
	for(i=0;i<aElem.length;i++)if(aElem[i].className == sClass)aClass.push(aElem[i]);
	return aClass;
}

//绑定事件
function addEvent(oElm, strEvent, fuc) {
    window.addEventListener ? oElm.addEventListener(strEvent, fuc, false) : oElm.attachEvent('on' + strEvent, fuc);
};

//初始化对象
function Tab(){
	this.initialize.apply(this, arguments);
}

Object.extend = function(destination, source){
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
};
Tab.prototype = {
	initialize : function(obj, dis, content, onEnd, eq){
		if($(obj)){
			this.obj = $(obj);
			this.oList = $$$(this.obj, 'list')[0];
			this.aCont = $$$(this.obj, content);
			this.oUl = $$(this.oList, 'ul')[0];
			this.aLi = this.oUl.children;
			this.timer = null;
			eq ? (this.aLi.length < eq  ? eq = 0 : eq) : eq = 0;
			this.oEve(onEnd);
			this.onEnd.method == 'mouseover' ? this.method = "mouseover" : this.method = "click";
			this.onEnd.autoplay == 'stop' ? this.autoplay = "stop" : this.autoplay = "play";
			this.aCont[eq].style.display = 'block';
			this.aLi[eq].className = 'hove';
			this.display(dis);
			this.autoPlayTab(eq, dis);
		}
	},
	
	oEve: function(onEnd){
        this.onEnd = {
            method: 'mouseover',
			autoplay: 'stop'
        };
        Object.extend(this.onEnd, onEnd || {});
    },
	
	//绑定事件
display : function(dis){
	var _this = this;
	var i = iNow = 0;
	for(i=0;i<this.aLi.length;i++){
		(function(){
			var j = i;
			addEvent(_this.aLi[j], _this.method,
			function() {
				_this.fnClick(j,dis);
				_this.autoPlayTab(j, dis);
			})
		})()
	}
	this.autoPlayTab(i, dis);
},
	
	//自动播放
	autoPlayTab : function(iNow, dis){
		if(this.autoplay == 'play'){
			var _this = this;
			this.iNow = iNow;
			this.obj.onmouseover = function(){
				clearInterval(_this.timer);
			};
			this.obj.onmouseout = function(){
				clearInterval(_this.timer);
				_this.timer = setInterval(playTab,3000);
			};
			clearInterval(_this.timer);
			_this.timer = setInterval(playTab,3000);
			function playTab(){
				if(_this.iNow == _this.aLi.length)_this.iNow = 0;
				_this.fnClick(_this.iNow, dis)
				_this.iNow++
			}
		}
	},
	//切换
	fnClick : function(oBtn, dis){
		var i = 0;
		for(i=0;i<this.aLi.length;i++)this.aLi[i].className = '',this.aCont[i].style.display = 'none';
		this.aLi[oBtn].className = dis;
		this.aCont[oBtn].style.display = 'block';
	}
};